import axios from 'axios'
import { ElMessage } from 'element-plus'
import {diffTokenTime} from "@/utils/auth.js"
import store from '@/store'

export const baseURL=process.env.VUE_APP_BASE_URL
const service=axios.create({
    baseURL,
    timeout:100000
    
})
service.interceptors.request.use(config=>{
    config.headers.Authorization=localStorage.getItem('token')
    console.log('request',localStorage.getItem('token'),diffTokenTime(),store)

    if(localStorage.getItem('token')&&diffTokenTime()){
        store.dispatch('app/loginOut')
        return Promise.resolve('token 失效')
    }else{
        return config
    }
   
},error=>{
    return Promise.reject(new Error(error))
})
service.interceptors.response.use(response=>{
    console.log(response,'response')
    const {data,meta}=response?.data
    if(data?.meta?.status==200 ){
        return Promise.resolve(data?.data)
    }else if(meta?.status){
        return Promise.resolve(data)
    }
    else{
        ElMessage({
            message: data?.meta?.msg,
            type: 'error',
          })
          return Promise.resolve(data?.meta?.msg)
    }

})
// ,error=>{
//     error?.response&& ElMessage({
//         message: error?.response.data,
//         type: 'error',
//       })
//       return Promise.resolve(new Error(error?.response.data))

// }
export default  service